package com.meterreadingsys.util;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil{
	
	/**
	 * 获取当前日期时间
	 * @return
	 */
    public static String getDateTime(){
        Date dt = new Date();
        long tm = dt.getTime();
        return ((new java.sql.Date(tm)) + " " + (new java.sql.Time(tm)));
    }

    /**
     * 获取日期
     * @return
     */
    public static String getDate()
    {
        Date dt = new Date();
        long tm = dt.getTime();
        return (new java.sql.Date(tm)).toString();
    }

   
	/**
	 * 得到当前日期前几天的日期
	 * @return yyyy-mm-dd
	 */
	public static String getBeforeDay(int num)
	{
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(new Date());
    calendar.add(Calendar.DATE, -num);
    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    return simpledateformat.format(calendar.getTime());
	}
	
	/**
	 * 得到给定日期后几日的日期
	 * @param num
	 * @return
	 */
	public static String getAfterDay(String date,int num)	{
        SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
        Date a = null;
		try {
			a = parser.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(a);
		calendar.add(Calendar.DATE, num);
		SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
		return simpledateformat.format(calendar.getTime());
	}
	
	/**
	 * 根据字符串得到日期型的日期
	 * @param day
	 * @return date
	 */
	public static Date getStringToDate(String day){
		SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd");
		Date a=new Date();
		try {
			a = parser.parse(day);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        return a;
	}
	
	/**
	 * 
	 * @Title: getSqlDate 
	 * @Description: 根据字符串得到SQL日期
	 * @param @param date 字符串日期
	 * @param @return
	 * @return java.sql.Date
	 * @throws
	 */
	public static java.sql.Date getSqlDate(String date){
		
		return new java.sql.Date(getStringToDate(date).getTime());
	}
	
	/**
	 * 
	 * @Title: getSqlTimestamp 
	 * @Description: 获取时间的毫秒值
	 * @param @return
	 * @return java.sql.Timestamp
	 * @throws
	 */
	public static java.sql.Timestamp getSqlTimestamp(){
		return new java.sql.Timestamp(new java.util.Date().getTime());
	}
	
	/**
	 * 
	 * @Title: getsqlDate 
	 * @Description: 获取当前系统SQL时间
	 * @param @return
	 * @return java.sql.Date
	 * @throws
	 */
	public static java.sql.Date getsqlDate(){
		return new java.sql.Date(new java.util.Date().getTime());
	}
}
